package app.qrcode.browser.chat.model

import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import app.qrcode.browser.chat.model.bean.InvitationInfo
import app.qrcode.browser.chat.model.bean.UserInfo
import app.qrcode.browser.chat.utils.Constant
import com.hyphenate.EMContactListener
import com.hyphenate.chat.EMClient


/**
 * Created by DomenCai on 2022/3/14.
 */
class EventListener(context: Context) {

    init {
        EMClient.getInstance().contactManager().setContactListener(object : EMContactListener {

            override fun onContactInvited(hxId: String, reason: String) {
                //收到好友邀请
                val invitationInfo = InvitationInfo(
                    UserInfo(null, hxId, null, null),
                    null,
                    reason,
                    InvitationInfo.InvitationStatus.NEW_INVITE
                )

                Model.dbManager.inviteTableDao.addInvitation(invitationInfo)
                Log.w("cdm", "EventListener.onContactInvited -> 收到好友邀请")
            }

            override fun onFriendRequestAccepted(hxId: String?) {
                val invitationInfo = InvitationInfo(
                    UserInfo(null, hxId, null, null),
                    null,
                    null,
                    InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER
                )
                Model.dbManager.inviteTableDao.addInvitation(invitationInfo)
                Log.w("cdm", "EventListener.onFriendRequestAccepted -> 同意了好友申请")
            }

            override fun onFriendRequestDeclined(p0: String?) {
                Log.w("cdm", "EventListener.onFriendRequestDeclined -> 拒绝了好友申请")
            }

            override fun onContactDeleted(hxId: String) {
                //被删除时回调此方法
                Model.dbManager.contactTableDao.deleteContactByHxId(hxId)
                Model.dbManager.inviteTableDao.removeInvitation(hxId)
                Log.w("cdm", "EventListener.onContactDeleted -> 删除了好友")
            }

            override fun onContactAdded(hxId: String) {
                //增加了联系人时回调此方法
                Model.dbManager.contactTableDao.saveContact(UserInfo(null, hxId, null, null), true)
                Log.w("cdm", "EventListener.onContactAdded -> 联系人增加了")
            }
        })
    }
}